package com.example.router

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.tencent.mmkv.MMKV

@Interceptor(priority = 5, name = "路由参数拦截器")
class RouteParamsInterceptor :IInterceptor{
    private lateinit var mContext: Context

    private val path = arrayOf("/activity/detail","/activity/list")
    override fun init(context: Context?) {
       if(context != null){
           mContext = context
       }
    }

    override fun process(postcard: Postcard, callback: InterceptorCallback) {
        var routePath = postcard.path
        var routeParams = postcard.extras
        var sessionId = MMKV.defaultMMKV().decodeString("sessionId","14532")
        if(path.contains(routePath) && !routeParams.containsKey("sessionId")){
            postcard.withString("sessionId",sessionId)
        }
        callback.onContinue(postcard)
    }
}